/*=============================================================================

  FGRAME GRABBER INTERFACE MODULE

  FILE: HFgDev.cpp
  
  DESCRIPTION

    Frame Grabber Interface Module Definition

  EXTERNALIZED FUNCTIONS

    
  Copyright (c) DaeSung Engineering Inc. All rights reserved.

=============================================================================*/
/*=============================================================================

                             EDIT HISTORY

  ---------  -------  -------------------------------------------------------
    when       who     what, where, why
  ---------  -------  -------------------------------------------------------
   07/17/04   nso4th   Frame Grabber Interface

=============================================================================*/

/*=============================================================================
  HEADER INCLUSION
=============================================================================*/
#include <Cust_AppDef.h>
#include "HFgDev.h"
#include <devSP/fgHal_met2mc.h>

/*=============================================================================
  CONSTANT DEFINITION
=============================================================================*/

/*=============================================================================
  LOCALIZED FUNCTIONS
=============================================================================*/
static dword FgDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser);

/*=============================================================================
  HFgDev CLASS IMPLEMENTATION
=============================================================================*/
/*-----------------------------------------------------------------------------
  CONSTRUCTOR & DESTRUCTOR
-----------------------------------------------------------------------------*/
HFgDev::HFgDev():HFgDevBase()
{
	Event_SetCallback(FgDevEventCallback, this);
}

HFgDev::~HFgDev()
{
	Close();
}

/*-----------------------------------------------------------------------------
  OPERATION
-----------------------------------------------------------------------------*/
boolean HFgDev::SetCamInfo(char *pszCam)
{
	int32		devID;
	int32		dx, dy;
	
	devID = GetCurrentDevice();
	
	if(FgDev_SetParam(m_pDev, FGDEV_CAM_INFO_P, (dword)pszCam, 0) == FALSE)
		return	FALSE;

	dx = (int32)FgDev_GetParam(m_pDevList[devID], FGDEV_WIDTH_P, 0, 0);
	dy = (int32)FgDev_GetParam(m_pDevList[devID], FGDEV_HEIGHT_P, 0, 0);
	if(m_pImg[devID])
	{
		IMP_FREE(m_pImg[devID]);
		m_pImg[devID] = NULL;
	}
	// Color
	if(FgDev_GetParam(m_pDevList[devID], FGDEV_CHANNUM_P, 0, 0) == 3)
	{
		m_dwMemSize[devID] = dx * dy * 3;  //Color
		m_pImg[devID] = IMP_ALLOC(IMG_TYPE_BIT24_3CHAN, dx, dy, TRUE);
	}
	else
	{
		m_dwMemSize[devID] = dx * dy;  //Gray
		m_pImg[devID] = IMP_ALLOC(IMG_TYPE_BIT8, dx, dy, TRUE);
	}

	return	TRUE;
}

/*=============================================================================
  EVENT HANDLER
=============================================================================*/
dword HFgDev::Event_Handler(dword dwEvent, dword wParam, dword lParam)
{
	if(Event_PreProc(dwEvent, wParam, lParam))
	{
		return	TRUE;
	}

	return	Event_Pass(dwEvent, wParam, lParam);
}

dword HFgDev::Event_PreProc(dword dwEvent, dword wParam, dword lParam)
{
	dword		event, evtID;

	event = HDEV_GETEVENT(dwEvent);
	evtID = HDEV_GETDEV_ID(dwEvent);
	
	switch(event)
	{
	case FGDEV_IMAGE_E:
	{
		memCopy(m_pImg[evtID]->pData, (byte*)wParam, m_dwMemSize[evtID]);

		if(MonFps_Toc(evtID))
		{
			double64		dFps = MonFps_Get(evtID);
			Event_Pass(HDEV_SETDEV_ID(HFGDEV_FPS_E, evtID), (dword)(&dFps), 0);
		}
		return	Event_Pass(dwEvent, (dword)(m_pImg[evtID]), 0);
	}
	default:
		break;
	}
	return	FALSE;
}

dword FgDevEventCallback(dword dwEvent, dword wParam, dword lParam, mcxt pUser)
{
	HDevCliCxt	*pCxt = (HDevCliCxt*)pUser;

	if(pCxt && pCxt->pThis)
	{
		return pCxt->pThis->Event_Handler(HDEV_SETDEV_ID(dwEvent, pCxt->dwDevID), wParam, lParam);
	}

	return	(0);
}
/*--- END OF HFgDev.cpp ---*/
